﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Ads;
using System.Collections.ObjectModel;
using Microsoft.Phone.Tasks;

namespace OmnipotentFlashlight
{
    public partial class Soft : PhoneApplicationPage
    {
        ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };
        int p = 1;
        bool isNextPage = true;
        int p2 = 1;
        bool isNextPage2 = true;
        ObservableCollection<AdsModel> lists = null;
        ObservableCollection<AdsModel> lists2 = null;
        public Soft()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            Loaded += new RoutedEventHandler(Soft_Loaded);
            list.LayoutUpdated += new EventHandler(list_LayoutUpdated);
            list2.LayoutUpdated += new EventHandler(list_LayoutUpdated2);
            list.SelectionChanged += new SelectionChangedEventHandler(list_SelectionChanged);
            list2.SelectionChanged += new SelectionChangedEventHandler(list2_SelectionChanged);
        }

        void list2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (list2.SelectedIndex >= 0)
            {
                string str = (list2.SelectedItem as AdsModel).appid;
                NavigationService.Navigate(new Uri("/SoftDetail.xaml?appid=" + str, UriKind.Relative));
            }
        }
        void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (list.SelectedIndex >= 0)
            {
                string str = (list.SelectedItem as AdsModel).appid;
                NavigationService.Navigate(new Uri("/SoftDetail.xaml?appid=" + str, UriKind.Relative));
            }
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            list.SelectedIndex = list2.SelectedIndex = -1;
            base.OnNavigatedFrom(e);
        }
        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (piv.SelectedIndex == 1 && lists2 == null)
            {
                SystemTray.ProgressIndicator = _progress;
                _progress.IsVisible = true;
                App.ads.OnGetAdsRequest += new Ads.AdsHelper.GetAdsRequest(ads_OnGetAdsRequest2);
                App.ads.GetAds(0, 1, 2);
            }
        }

        void Soft_Loaded(object sender, RoutedEventArgs e)
        {
            if (lists == null)
            {
                SystemTray.ProgressIndicator = _progress;
                _progress.IsVisible = true;
                App.ads.OnGetAdsRequest += new Ads.AdsHelper.GetAdsRequest(ads_OnGetAdsRequest);
                App.ads.GetAds(0, 1, 1);
            }
        }
        void ads_OnGetAdsRequest(Ads.AdsModels ads)
        {
            if (ads == null || ads.list == null || ads.list.Count <= 0) { _progress.IsVisible = false; return; };
            lists = new ObservableCollection<AdsModel>(ads.list);
            list.ItemsSource = lists;
            isNextPage = ads.isnextpage == "1";
            p = ads.p;
            _progress.IsVisible = false;
            App.ads.OnGetAdsRequest -= ads_OnGetAdsRequest;
        }
        void ads_OnGetAdsRequest2(Ads.AdsModels ads)
        {
            if (ads.list.Count <= 0) { _progress.IsVisible = false; s_loading2.Text = "暂无数据"; return; };
            if (s_loading2.Visibility == Visibility.Visible)
            {
                s_loading2.Visibility = Visibility.Collapsed;
                list2.Visibility = Visibility.Visible;
            }
            lists2 = new ObservableCollection<AdsModel>(ads.list);
            list2.ItemsSource = lists2;
            isNextPage2 = ads.isnextpage == "1";
            p2 = ads.p;
            _progress.IsVisible = false;
            App.ads.OnGetAdsRequest -= ads_OnGetAdsRequest2;
        }
        void list_LayoutUpdated(object sender, EventArgs e)
        {
            ScrollViewer scrollViewer = FindChildOfType<ScrollViewer>(list);
            if (scrollViewer != null)
            {
                if (isNextPage && !_progress.IsVisible &&
                    scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight &&
                    list.Items.Count > 0)
                {
                    _progress.IsVisible = true;
                    App.ads.OnGetAdsRequest += new Ads.AdsHelper.GetAdsRequest(ads_nextPage);
                    App.ads.GetAds(0, p + 1, 1);
                }
            }
        }
        void list_LayoutUpdated2(object sender, EventArgs e)
        {
            ScrollViewer scrollViewer = FindChildOfType<ScrollViewer>(list);
            if (scrollViewer != null)
            {
                if (isNextPage2 && !_progress.IsVisible &&
                    scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight &&
                    list2.Items.Count > 0)
                {
                    _progress.IsVisible = true;
                    App.ads.OnGetAdsRequest += new Ads.AdsHelper.GetAdsRequest(ads_nextPage2);
                    App.ads.GetAds(0, p2 + 1, 2);
                }
            }
        }
        void ads_nextPage(Ads.AdsModels ads)
        {
            if (ads.list.Count <= 0) { _progress.IsVisible = false; return; }
            p = ads.p;
            isNextPage = ads.isnextpage == "1";
            foreach (AdsModel ad in ads.list)
                lists.Add(ad);
            _progress.IsVisible = false;
            App.ads.OnGetAdsRequest -= ads_nextPage;
        }
        void ads_nextPage2(Ads.AdsModels ads)
        {
            if (ads.list.Count <= 0) { _progress.IsVisible = false; return; }
            p2 = ads.p;
            isNextPage2 = ads.isnextpage == "1";
            foreach (AdsModel ad in ads.list)
                lists2.Add(ad);
            _progress.IsVisible = false;
            App.ads.OnGetAdsRequest -= ads_nextPage2;
        }
        static T FindChildOfType<T>(DependencyObject root) where T : class
        {
            var queue = new Queue<DependencyObject>();
            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                DependencyObject current = queue.Dequeue();
                for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
                {
                    var child = VisualTreeHelper.GetChild(current, i);
                    var typedChild = child as T;
                    if (typedChild != null)
                    {
                        return typedChild;
                    }
                    queue.Enqueue(child);
                }
            }
            return null;
        }



    }
}